package sharetime.playlist.service;

import org.hibernate.validator.InvalidValue;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import sharetime.core.AnnotationValidator;
import sharetime.playlist.domain.Playlist;

public class PlaylistValidator implements Validator {

	@SuppressWarnings("unchecked")
	@Override
	public boolean supports(Class clazz) {
		return Playlist.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		Playlist playlist = (Playlist) target;
		InvalidValue[] invalids = AnnotationValidator
				.getInvalidValues(playlist);
		for (InvalidValue invalidValue : invalids) {
			errors.rejectValue(invalidValue.getPropertyPath(), null,
					invalidValue.getMessage());
		}
	}

}
